//
//  GeexRecorderNetAccessor.m
//  VideoRecordManager
//
//  Created by zhangbaochuan on 2020/2/27.
//  Copyright © 2020 WangYonghe. All rights reserved.
//

#import "GeexRecorderNetAccessor.h"
#import "GeexRecorder.h"

//beta
//https://cjh.geexwin.com/api-gateway/api-vcard/file/restApi/uploadBinaryFile
//
//生产
//https://chaojihua.geexwin.com/api-gateway/api-vcard/file/restApi/uploadBinaryFile

#if DEBUG

#define apiRecordHost @"https://cjh.geexwin.com/api-gateway/api-vcard/file/"
#else

#define apiRecordHost @"https://chaojihua.geexwin.com/api-gateway/api-vcard/file/"

#endif

@implementation GeexRecorderNetAccessor

+ (instancetype)sharedInstance {
    static GeexRecorderNetAccessor *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (void)sendVidioFormUrl:(NSString *)urlStr filedata:(NSData *)data parameters:(id)parameters process:(ProgressBlock)progress finished:(FinishedBlock)finished
{
    NSString *partURL = apiRecordHost;
    NSString *URLString = [partURL stringByAppendingString:urlStr];

    AFHTTPSessionManager *_manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    _manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    _manager.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型
    
    _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                            @"text/html",
                                                            @"text/json",
                                                            @"text/plain",
                                                            @"text/javascript",
                                                            @"text/xml",
                                                            @"image/*",nil];//如果报接受类型不一致请替换一致text/html或别的
        ///允许返回的结果可改
        //    _manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableContainers];
    _manager.requestSerializer=[AFJSONRequestSerializer serializer];//申明请求的数据是json类型
    _manager.requestSerializer.timeoutInterval = 60;
      
        //    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [_manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"platform"];
    [_manager.requestSerializer setValue:@"1.0" forHTTPHeaderField:@"versionCode"];
    [_manager.requestSerializer setValue: [NSString stringWithFormat:@"Bearer %@",[GeexRecorder shared].token] forHTTPHeaderField:@"Authorization"];
    [_manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [_manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [_manager.requestSerializer setStringEncoding:NSUTF8StringEncoding];
    
    
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    [_manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *fileName = [NSString stringWithFormat:@"authVideo_%@.mp4",[formatter stringFromDate:[NSDate date]]];
//        application/octet-stream
        [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"video/mp4"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress != nil) {
            progress(uploadProgress);
        }
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        finished(Enum_SUCCESS,responseObject);
        [AFNetworkActivityIndicatorManager sharedManager].enabled = NO;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        finished(Enum_FAIL,error);
        NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"服务器的错误原因:%@",str);
        [AFNetworkActivityIndicatorManager sharedManager].enabled = NO;
    }];
    
}

@end
